<template>
	<el-container>
		<el-header class="header-tabs">
			<el-tabs type="card" v-model="groupId" @tab-change="tabChange">
				<el-tab-pane label="采购入库单列表" name="1"></el-tab-pane>
				<el-tab-pane label="采购入库商品明细" name="2"></el-tab-pane>
			</el-tabs>
		</el-header>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'inorder.add'" v-if="groupId == '1'"></el-button>
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" v-if="groupId == '1'"
						   @click="batch_del" v-auth="'inorder.delete'"></el-button>
				<!-- <el-button type="success" :disabled="selection.length == 0" @click="batch_check">批量审核</el-button> -->
				<el-popconfirm title="此操作会变更库存数量,是否继续?" @confirm="batch_check" :width="200" v-if="groupId == '1'">
					<template #reference>
						<el-button type="warning" plain v-auth="'inorder.check'" :disabled="selection.length == 0">审核</el-button>
					</template>
				</el-popconfirm>
				<el-popconfirm title="此操作会变更库存数量,是否继续?" @confirm="batch_uncheck" v-if="groupId == '1'">
					<template #reference>
						<el-button type="info" plain :disabled="selection.length == 0" v-auth="'inorder.uncheck'">反审核</el-button>
					</template>
				</el-popconfirm>
				<sc-export-button :auth="'inorder.export'" :apiObj="apiObj" :search="search" v-if="groupId == 1"></sc-export-button>
				<sc-export-button :auth="'inorder.detailsExport'" :apiObj="shopObj" :search="search" v-if="groupId == 2"></sc-export-button>
				<!-- 打印-->
				<sc-print-button :auth="'inorder.print'" :selection="selection"></sc-print-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-date-picker v-model="search.entry_date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 230px"/>
					<el-input v-model="search.purchase_code" placeholder="采购入库单号" clearable @keyup.enter="upsearch"></el-input>
					<el-input v-model="search.goods_name" placeholder="请输入商品名称" clearable @keyup.enter="upsearch" v-if="groupId == 2"></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					<el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding" v-if="groupId == 1">
			<scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" border>
				<el-table-column type="selection" width="50" align="center"></el-table-column>
				<el-table-column type="index" width="50" align="center"></el-table-column>
				<el-table-column label="采购入库单号" prop="purchase_code" align="center"></el-table-column>
				<!-- <el-table-column label="供应商" prop="purchase_code" align="center"></el-table-column> -->
				<el-table-column label="开单日期" prop="open_date" align="center"></el-table-column>
				<el-table-column label="采购单号" prop="purchase.code" align="center"></el-table-column>
				<el-table-column label="入库日期" prop="entry_date" align="center"></el-table-column>
				<el-table-column label="备注" prop="remark" align="center"></el-table-column>
				<el-table-column label="收货人" prop="collect.name" align="center"></el-table-column>
				<el-table-column label="创建人" prop="admin.name" align="center"></el-table-column>
				<el-table-column label="创建时间" prop="create_time" align="center"></el-table-column>
				<el-table-column label="审核状态" prop="status" align="center">
					<template #default="scope">
						<el-tag :type="scope.row.status == 0 ? 'warning' : 'success'">{{ scope.row.status == 0 ? '待审核' : '已审核' }}</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" width="210">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_show(scope.row, scope.$index)" v-auth="'inorder.show'">查看</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'inorder.edit'" v-if="scope.row.status == 0">编辑
							</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="danger" size="small" v-auth="'inorder.delete'" v-if="scope.row.status == 0">删除</el-button>
								</template>
							</el-popconfirm>
							<!-- <el-popconfirm title="此操作会变更库存数量,是否继续?" @confirm="table_check(scope.row)">
				<template #reference>
				  <el-button text type="success" size="small" v-auth="'inorder.check'" v-if="scope.row.status == 0">审核</el-button>
				</template>
			  </el-popconfirm> -->
							<!-- <el-popconfirm title="此操作会变更库存数量,是否继续?" @confirm="table_uncheck(scope.row)">
				<template #reference>
				  <el-button text type="warning" size="small" v-auth="'inorder.uncheck'" v-if="scope.row.status == 1">反审核</el-button>
				</template>
			  </el-popconfirm> -->
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
		<el-main class="nopadding" v-if="groupId == 2">
			<scTable ref="table" row-key="id" :apiObj="shopObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border>
				<el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
				<el-table-column type="index" label="序号" fixed align="center" width="50"></el-table-column>
				<el-table-column label="采购入库单号" prop="purchase_code" align="center"></el-table-column>
				<el-table-column label="开单日期" prop="open_date" align="center"></el-table-column>
				<el-table-column label="采购单号" prop="purchase.code" align="center"></el-table-column>
				<el-table-column label="入库日期" prop="entry_date" align="center"></el-table-column>
				<el-table-column label="商品名称" prop="goods_name" align="center"></el-table-column>
				<el-table-column label="商品编码" prop="goods_code" align="center"></el-table-column>
				<el-table-column label="商品规格" prop="goods_spec" align="center"></el-table-column>
				<el-table-column label="商品单位" prop="unit_name" align="center"></el-table-column>
				<el-table-column label="商品分类" prop="cate_name" align="center"></el-table-column>
				<el-table-column label="数量" prop="number" align="center"></el-table-column>
				<el-table-column label="创建时间" prop="create_time" align="center"></el-table-column>
			</scTable>
		</el-main>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
	<check-status v-if="dialog.check" ref="checkDialog" @success="handleSuccess" @closed="dialog.check = false" draggable></check-status>
</template>

<script>
import CheckStatus from '@/views/ware/checkStatus.vue'
import saveDialog from './save'

export default {
	name: '采购计划管理',
	components: {
		saveDialog,
		CheckStatus
	},
	data() {
		return {
			dialog: {
				save: false,
				check: false,
			},
			apiObj: this.$API.procure.inorder.list,
			shopObj: this.$API.procure.inorder.goodsLists,
			selection: [],
			search: {
				purchase_code: null,
				goods_name:null
			},
			query: {
				type: 1
			},
			arrival: [],
			factory: [],
			groupId: "1",
		}
	},
	methods: {
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		// 转采购单
		table_turn(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('turn').setData(row)
			})
		},
		//查看
		table_show(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('show').setData(row)
			})
		},
		//删除
		async table_del(row) {
			var reqData = {id: row.id}
			var res = await this.$API.procure.inorder.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async table_del_all(delete_ids) {
			var reqData = {id: delete_ids}
			var res = await this.$API.procure.inorder.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})

			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'inorder') {
				this.$refs.table.refresh()
			} else if (mode == 'check') {
				console.log('data :>> ', data);
				this.table_check(data[0])
			}
		},
		refresh() {
			for (const key in this.search) {
				this.search[key] = null;
			}
			this.$refs.table.upData(this.search)
		},
		batch_check() {
			var ids = [];
			let go = true
			this.selection.forEach((item) => {
				if (item.status != 0) {
					go = false
				} else {
					ids.push(item.id)
				}
			})
			if (!go) {
				this.$message.error("请选择未审核的订单")
				return
			}
			this.table_check(ids)
		},
		// 审核
		async table_check(row) {
			console.log('row :>> ', row);
			var reqData = {id: row, status: 1}
			var res = await this.$API.procure.inorder.check.get(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		batch_uncheck() {
			var ids = [];
			let go = true
			this.selection.forEach((item) => {
				if (item.status != 1) {
					go = false
				} else {
					ids.push(item.id)
				}
			})
			if (!go) {
				this.$message.error("请选择已审核的订单")
				return
			}
			this.table_uncheck(ids)
		},
		// 反审核
		async table_uncheck(row) {
			var reqData = {id: row}
			var res = await this.$API.procure.inorder.uncheck.get(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("操作成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
	}
}
</script>

<style>
</style>
